import { Injectable } from '@angular/core';
import { PROVINCE } from './lesson6-json';
import { Subject, Observable } from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class Lesson6ServiceService {

  private province = PROVINCE;  // 所有省份数据

  private toUpDateDataSubject = new Subject<any>();

  upData: Observable<any>;

  leftData = [];

  rightData = [];

  constructor() {
    this.leftData = this.province;
    this.upData = this.toUpDateDataSubject.asObservable();
  }

  LTR() {
    const arr = this.leftData.filter(v => v.check);
    this.rightData = this.rightData.concat(arr);
    this.removeItem('l', arr);
    this.toUpData();
  }

  RTL() {
    const arr = this.rightData.filter(v => v.check);
    this.leftData = this.leftData.concat(arr);
    this.removeItem('r', arr);
    this.toUpData();
  }

  removeItem(type: string, arr) {
    const data = type === 'l' ? this.leftData : this.rightData;
    arr.forEach((v) => {
      const index = data.find(value => value.name === v.name);
      data.splice(index, 1);
    });
  }

  toUpData() {
    this.toUpDateDataSubject.next();
  }
}
